package com.hot12345.springmvc;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.hot12345.common.basedata.Constant;
import com.hot12345.common.spring.SpringAbstractController;
import com.hot12345.dao.InfoDaoImpl;
import com.hot12345.dao.ItemSiteDaoImpl;
import com.hot12345.entity.Info;
import com.hot12345.entity.Item;
import com.hot12345.entity.ItemSite;
import com.hot12345.service.InfoServiceImpl;
import com.hot12345.service.ItemServiceImpl;
import com.hot12345.vo.InfoVO;

@Controller
@RequestMapping("/index")
public class IndexController extends SpringAbstractController {
	
	private final static Log log = LogFactory.getLog(IndexController.class);

	@RequestMapping("")
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		/**
//		Map<String,Object> modelMap = new HashMap<String,Object>();
		InfoServiceImpl infoSer = InfoServiceImpl.getInstance();
		
		
		List<InfoVO> lastTopAllList = infoSer.getInfoList(Constant.dateType_24Hour,null, 0, 18);
		modelMap.put("lastTopAllList", lastTopAllList);
		
		List<InfoVO> weekTopAllList = infoSer.getInfoList(Constant.dateType_week,null, 0, 18);
		modelMap.put("weekTopAllList", weekTopAllList);
		
		List<InfoVO> monthTopAllList = infoSer.getInfoList(Constant.dateType_month,null, 0, 18);
		modelMap.put("monthTopAllList", monthTopAllList);
		
		List<InfoVO> newsList = infoSer.getInfoList(Constant.dateType_last,1, 0, 10);
		modelMap.put("newsList", newsList);
		
		List<InfoVO> newsWeekList = infoSer.getInfoList(Constant.dateType_week, 1, 0, 10);
		modelMap.put("newsWeekList", newsWeekList);
		
		List<InfoVO> newsMonthList = infoSer.getInfoList(Constant.dateType_month, 1, 0, 10);
		modelMap.put("newsMonthList", newsMonthList);
		
		List<InfoVO> yuleLastList = infoSer.getInfoList(Constant.dateType_last, 7, 0, 10);
		modelMap.put("yuleLastList", yuleLastList);
		
		List<InfoVO> yuleWeekList = infoSer.getInfoList(Constant.dateType_week, 7, 0, 10);
		modelMap.put("yuleWeekList", yuleWeekList);
		
		List<InfoVO> yuleMonthList = infoSer.getInfoList(Constant.dateType_month, 7, 0, 10);
		modelMap.put("yuleMonthList", yuleMonthList);
		
		//体育
		List<InfoVO> sportsLastList = infoSer.getInfoList(Constant.dateType_last, 6, 0, 10);
		modelMap.put("sportsLastList", sportsLastList);
		
		List<InfoVO> sportsWeekList = infoSer.getInfoList(Constant.dateType_week, 6, 0, 10);
		modelMap.put("sportsWeekList", sportsWeekList);
		
		List<InfoVO> sportsMonthList = infoSer.getInfoList(Constant.dateType_month, 6, 0, 10);
		modelMap.put("sportsMonthList", sportsMonthList);
		
		//热贴
		List<InfoVO> bbsLastList = infoSer.getInfoList(Constant.dateType_last, 2, 0, 18);
		modelMap.put("bbsLastList", bbsLastList);
		
		List<InfoVO> bbsWeekList = infoSer.getInfoList(Constant.dateType_week, 2, 0, 18);
		modelMap.put("bbsWeekList", bbsWeekList);
		
		List<InfoVO> bbsMonthList = infoSer.getInfoList(Constant.dateType_month, 2, 0, 18);
		modelMap.put("bbsMonthList", bbsMonthList);
		
		//点评
		List<InfoVO> dianpinLastList = infoSer.getInfoList(Constant.dateType_last, 8, 0, 5);
		modelMap.put("dianpinLastList", dianpinLastList);
		
		List<InfoVO> dianpinWeekList = infoSer.getInfoList(Constant.dateType_week, 8, 0, 5);
		modelMap.put("dianpinWeekList", dianpinWeekList);
		
		List<InfoVO> dianpinMonthList = infoSer.getInfoList(Constant.dateType_month, 8, 0, 5);
		modelMap.put("dianpinMonthList", dianpinMonthList);
		
		//电影
		List<InfoVO> videoLastList = infoSer.getInfoList(Constant.dateType_last, 11, 0, 10);
		modelMap.put("videoLastList", videoLastList);
		
		List<InfoVO> videoWeekList = infoSer.getInfoList(Constant.dateType_week, 11, 0, 10);
		modelMap.put("videoWeekList", videoWeekList);
		
		List<InfoVO> videoMonthList = infoSer.getInfoList(Constant.dateType_month, 11, 0, 10);
		modelMap.put("videoMonthList", videoMonthList);
		
		//军事
		List<InfoVO> junshiLastList = infoSer.getInfoList(Constant.dateType_last, 5, 0, 10);
		modelMap.put("junshiLastList", junshiLastList);
		
		List<InfoVO> junshiWeekList = infoSer.getInfoList(Constant.dateType_week, 5, 0, 10);
		modelMap.put("junshiWeekList", junshiWeekList);
		
		List<InfoVO> junshiMonthList = infoSer.getInfoList(Constant.dateType_month, 5, 0, 10);
		modelMap.put("junshiMonthList", junshiMonthList);
		
		//视频
		List<InfoVO> movieLastList = infoSer.getInfoList(Constant.dateType_last, 12, 0, 10);
		modelMap.put("movieLastList", movieLastList);
		
		List<InfoVO> movieWeekList = infoSer.getInfoList(Constant.dateType_week, 12, 0, 10);
		modelMap.put("movieWeekList", movieWeekList);
		
		List<InfoVO> movieMonthList = infoSer.getInfoList(Constant.dateType_month, 12, 0, 10);
		modelMap.put("movieMonthList", movieMonthList);
		
//		//游戏
//		List<InfoVO> gameLastList = infoSer.getInfoList(Constant.dateType_last, 3, 0, 10);
//		modelMap.put("gameLastList", gameLastList);
//		
//		List<InfoVO> gameWeekList = infoSer.getInfoList(Constant.dateType_week, 3, 0, 10);
//		modelMap.put("gameWeekList", gameWeekList);
//		
//		List<InfoVO> gameMonthList = infoSer.getInfoList(Constant.dateType_month, 3, 0, 10);
//		modelMap.put("gameMonthList", gameMonthList);
		
		//游戏资讯
		List<InfoVO> gamenewsLastList = infoSer.getInfoList(Constant.dateType_last, 15, 0, 10);
		modelMap.put("gamenewsLastList", gamenewsLastList);
		
		List<InfoVO> gamenewsWeekList = infoSer.getInfoList(Constant.dateType_week, 15, 0, 10);
		modelMap.put("gamenewsWeekList", gamenewsWeekList);
		
		List<InfoVO> gamenewsMonthList = infoSer.getInfoList(Constant.dateType_month, 15, 0, 10);
		modelMap.put("gamenewsMonthList", gamenewsMonthList);
		
		//连续剧
		List<InfoVO> tvLastList = infoSer.getInfoList(Constant.dateType_last, 13, 0, 10);
		modelMap.put("tvLastList", tvLastList);
		
		List<InfoVO> tvWeekList = infoSer.getInfoList(Constant.dateType_week, 13, 0, 10);
		modelMap.put("tvWeekList", tvWeekList);
		
		List<InfoVO> tvMonthList = infoSer.getInfoList(Constant.dateType_month, 13, 0, 10);
		modelMap.put("tvMonthList", tvMonthList);
		
		//小说
//		List<InfoVO> xiaoshuoLastList = infoSer.getInfoList(Constant.dateType_last, 9, 0, 10);
//		modelMap.put("xiaoshuoLastList", xiaoshuoLastList);
//		
//		List<InfoVO> xiaoshuoWeekList = infoSer.getInfoList(Constant.dateType_week, 9, 0, 10);
//		modelMap.put("xiaoshuoWeekList", xiaoshuoWeekList);
//		
//		List<InfoVO> xiaoshuoMonthList = infoSer.getInfoList(Constant.dateType_month, 9, 0, 10);
//		modelMap.put("xiaoshuoMonthList", xiaoshuoMonthList);
		
		List<InfoVO> societyLastList = infoSer.getInfoList(Constant.dateType_last, 14, 0, 10);
		modelMap.put("societyLastList", societyLastList);
		
		List<InfoVO> societyWeekList = infoSer.getInfoList(Constant.dateType_week, 14, 0, 10);
		modelMap.put("societyWeekList", societyWeekList);
		
		List<InfoVO> societyMonthList = infoSer.getInfoList(Constant.dateType_month, 14, 0, 10);
		modelMap.put("societyMonthList", societyMonthList);
		
		//情感
		List<InfoVO> ganqingLastList = infoSer.getInfoList(Constant.dateType_last, 4, 0, 10);
		modelMap.put("ganqingLastList", ganqingLastList);
		
		List<InfoVO> ganqingWeekList = infoSer.getInfoList(Constant.dateType_week, 4, 0, 10);
		modelMap.put("ganqingWeekList", ganqingWeekList);
		
		List<InfoVO> ganqingMonthList = infoSer.getInfoList(Constant.dateType_month, 4, 0, 10);
		modelMap.put("ganqingMonthList", ganqingMonthList);
		
		//情感
		List<InfoVO> kaixinLastList = infoSer.getInfoList(Constant.dateType_last, 16, 0, 10);
		modelMap.put("kaixinLastList", kaixinLastList);
		
		List<InfoVO> kaixinWeekList = infoSer.getInfoList(Constant.dateType_week, 16, 0, 10);
		modelMap.put("kaixinWeekList", kaixinWeekList);
		
		List<InfoVO> kaixinMonthList = infoSer.getInfoList(Constant.dateType_month, 16, 0, 10);
		modelMap.put("kaixinMonthList", kaixinMonthList);
		
		modelMap.put("isIndex", true);
		*/
		return new ModelAndView("home/index");
	}
	
	@RequestMapping(value="/paihang/{pathKey}", method=RequestMethod.GET)
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap,@PathVariable("pathKey") String pathKey) throws Exception {
		
		Item item = null;
		if("rank".equals(pathKey)){
			item = new Item();
			item.setName("总关注排行榜");
			item.setTitle("总关注排行榜");
			item.setKeywords("热点排行榜,总排行榜");
			item.setDescription("总排行榜聚合全站被关注最多的内容，是热点中的热点，总排行榜的内容类型不限，即有热点新闻，社会新闻，也有娱乐，体育，情感，帖子，电影，电视剧，游戏等等。");
		}
		
		if(item==null){
			log.debug("pathKey="+pathKey);
			ItemServiceImpl itemSer = ItemServiceImpl.getInsantce();
			item = itemSer.getItemByPathKey(pathKey);
			if(item==null){
				log.error("栏目不存在,pathKey="+pathKey);
				//throw new Exception("栏目不存在");
				item = new Item();
				item.setName("总关注排行榜");
				item.setTitle("总关注排行榜");
				item.setKeywords("热点排行榜,总排行榜");
				item.setDescription("总排行榜聚合全站被关注最多的内容，是热点中的热点，总排行榜的站点及内容类型都不限，即有热点新闻，社会新闻，也有娱乐，体育，情感，帖子，电影，电视剧，游戏等等。");
			}
		}
		modelMap.put("item", item);
		
		InfoServiceImpl infoSer = InfoServiceImpl.getInstance();
		List<InfoVO> hour24List = infoSer.getInfoList(Constant.dateType_last, item.getId(), 0, 29);
		modelMap.put("info24hourList", hour24List);
		
		List<InfoVO> infoWeekList = infoSer.getInfoList(Constant.dateType_week, item.getId(), 0, 29);
		modelMap.put("infoWeekList", infoWeekList);
		
		List<InfoVO> infoMonthList = infoSer.getInfoList(Constant.dateType_month, item.getId(), 0, 29);
		modelMap.put("infoMonthList", infoMonthList);
		
		List<InfoVO> info6MonthList = infoSer.getInfoList(Constant.dateType_6month, item.getId(), 0, 29);
		modelMap.put("info6MonthList", info6MonthList);
		
		Map<Integer, String> navNameMap = Constant.getItemNav();
		String navName = navNameMap.get(item.getId());
		modelMap.put("navName", navName);
		
		if(item.getId()!=null){//显示站点导航,总排行榜不需要显示，所以不需要传栏目ID
			ItemSiteDaoImpl itemSiteDao = ItemSiteDaoImpl.getInstance();
			List<ItemSite> itemSiteList = itemSiteDao.getItemAll(item.getId(),1);
			modelMap.put("itemSiteList", itemSiteList);
		}
		return new ModelAndView("home/list");
	}
	
	/**
	 * 如果用户指定浏览某个站点的信息，那么通过这个方法来跳转。
	 *	
	 * @param request
	 * @param response
	 * @param modelMap
	 * @throws Exception
	 * @author wuzongbao
	 * @date 2013-3-11 下午09:40:53
	 * @version V1.0
	 */
	@RequestMapping(value="/view",method=RequestMethod.GET)
	public void view(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		String siteID_str = request.getParameter("siteID");
		String id_str = request.getParameter("id");
//		if(id_str==null){
//			log.error("信息ID为空");
//			throw new Exception("infoID is null");
//		}
		
		Long infoID = null;
		try{
			infoID = Long.valueOf(id_str);
		}catch(Exception e){
			log.error("转换信息ID异常：",e);
			throw new Exception(e);
		}
		
		if(infoID==null){
			log.error("信息ID为空");
			throw new Exception("信息ID为空");
		}
		
		int siteID = 0;
		if(siteID_str!=null){
			try{
				siteID = Integer.valueOf(siteID_str);
			}catch(Exception e){
				log.error("转换站点ID异常：", e);
			}
		}
		
		InfoDaoImpl infoDao = InfoDaoImpl.getInstance();
		Info info = infoDao.findByPrimaryKey(infoID);
		if(info==null){
			log.error("信息不存在，信息ID="+infoID);
			throw new Exception("信息不存在");
		}
		String redirectURL = info.getUrl();
		
		if(siteID==0){
			siteID = info.getSiteID();//如果站点ID为空，跳转到当前信息的站点
		}
		InfoServiceImpl infoSer = InfoServiceImpl.getInstance();
		Info siteInfo = infoSer.getInfoBySite(infoID, siteID);
		if(siteInfo!=null && siteInfo.getUrl()!=null && !"".equals(siteInfo.getUrl().trim())){
			redirectURL = siteInfo.getUrl();
		}
		
		
		
		response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
		response.setHeader("Location", redirectURL);
		return;
	}
}
